package lv.accenture.jbcamp.ex08;

public class Producer implements Runnable {

	int sizeOfJobs;
	int numberOfJobs;
	int delayBetweenJobs;
	String producerName;
	
	public Producer(int delayBetweenJobs, int numberOfJobs,
			String producerName, int sizeOfJobs) {
		super();
		this.delayBetweenJobs = delayBetweenJobs;
		this.numberOfJobs = numberOfJobs;
		this.producerName = producerName;
		this.sizeOfJobs = sizeOfJobs;
	}
	
	@Override
	public void run() {
		int i = 0;
		while (i < this.numberOfJobs) {
			PrintJob job = new PrintJob("Job : producer " + this.producerName + ", jobs number " + this.numberOfJobs, this.sizeOfJobs);
			if (Printer.getPrinter().addJob(job)) {
				System.out.println("\"" + job.getName() + "\" added to queue.");
				++i;
			} else {
				System.err.println("Queue is full.");
			}
			try {
				Thread.sleep(this.delayBetweenJobs);
			} catch (InterruptedException e1){
				System.err.println("Producer ERROR in SLEEP");
			}
		}
	}

}
